package com.huobao.yunying.utils;

import java.util.Map;

import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpUtil;

import android.content.Context;

public class SessionUtils {
	/**
	 */
	public static final long SESSION = 4 * 3600 * 1000;
	/**
	 */
	public static final long SESSION_TENDAY = 10 * 24 * 3600 * 1000;
	private static final String SESSION_KEY = "session_key";
	private static final String TENDAY = "tenday";

	/**
	 *
	 * @param context
	 * @param isTenday
	 */
	public static void login(Context context, boolean isTenday) {
		SharedPreferencesUtils.saveObject(context, SESSION_KEY, System.currentTimeMillis());
		SharedPreferencesUtils.saveObject(context, TENDAY, isTenday);

	}
	public static void saveUser(Context context, String username, String pwd, int role){
		SharedPreferencesUtils.saveObject(context, "username", username);
		SharedPreferencesUtils.saveObject(context, "pwd", pwd);
		SharedPreferencesUtils.saveObject(context, "role", role);
	}
	public static String getUserName(Context context){
		return (String) SharedPreferencesUtils.readObject(context, "username");
	}
	public static String getPwd(Context context){
		return (String) SharedPreferencesUtils.readObject(context, "pwd");
	}
	public static int getRole(Context context){
		Object ob = SharedPreferencesUtils.readObject(context, "role");
		if(ob == null || ob.equals(""))
			return -1;
		return (Integer) ob;
	}
	public static void clearPwd(Context context){
		SharedPreferencesUtils.saveObject(context, "username", "");
		SharedPreferencesUtils.saveObject(context, "pwd", "");
		SharedPreferencesUtils.saveObject(context, "role", -1);
	}
	
	

	/**
	 *
	 * @param context
	 * @return
	 */
	public static boolean isTenday(Context context) {
		Object object = SharedPreferencesUtils.readObject(context, TENDAY);
		if (object == null || object.equals("")) {
			return false;
		}
		boolean tenday = (Boolean) object;
		return tenday;
	}

	public static void loginout(Context context) {
		SharedPreferencesUtils.saveObject(context, SESSION_KEY, null);
		SharedPreferencesUtils.saveObject(context, TENDAY, false);
		clearPwd(context);
		Map<String, String> map = HttpUtil.getParams("app","login","act","logout");
		Http.getInstant().sendRequest(context, map);
	}

	public static boolean checkLogin(Context context) {
		Object ob = SharedPreferencesUtils.readObject(context, SESSION_KEY);
		long time = 0;
		if (ob == null || ob.equals("")) {
			time = 0;
		} else {
			time = (Long) ob;
		}
		if(isTenday(context)){
			return checkTendayLogin(context);
		}
		if ((System.currentTimeMillis() - time) > SESSION) {
			return false;
		}

		return true;
	}
	public static boolean checkTendayLogin(Context context) {
		Object ob = SharedPreferencesUtils.readObject(context, SESSION_KEY);
		long time = 0;
		if (ob == null || ob.equals("")) {
			time = 0;
		} else {
			time = (Long) ob;
		}
		if ((System.currentTimeMillis() - time) > SESSION_TENDAY) {
			return false;
		}

		return true;
	}
}
